<?php
/**
 * Created by PhpStorm.
 * User: Wang YuLong
 * Date: 2018/11/7
 * Time: 17:05
 */
namespace app\index\model;

use think\Model;

class Classify extends Model
{
    protected $resultSetType = 'collection';

    /**
     * 商品分类的展示
     * @return array
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $info = $this->all();
        foreach($info as $key)
        {
            $data[]=$key->toArray();
        }
        return $data;
    }

    /**
     * 商品分类的添加
     * @param $data
     * @return false|int
     * @throws \think\exception\DbException
     */
    public function adddo($data)
    {
        $sort = Classify::all()->column('sort');
        $sort = Max($sort);
        $data['sort']=$sort+1;
        $info = new Classify($data);
        return $info->allowField(true)->save();
    }

    /**
     * 执行修改商品分类的操作
     * @param $data
     * @return false|int
     */
    public function moddo($data)
    {
        $classify = new Classify();
        $name = $data['name'];
        $sort    = $data['sort'];
        $id        = $data[ 'id'];
        return $classify->allowField(true)->save(['name'=>$name,'sort'=>$sort], ['id' => $id]);
    }
}